﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class CycleCountReleaseBLL
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private static readonly ICycleCountReleaseDA CycleCountReleaseDA = DataAccess.CreateCycleCountReleaseDA();
        private static readonly ICycleCountTagDA CycleCountTagDA = DataAccess.CreateCycleCountTagDA();
        private List<string> dataBaseID;
        private PubParasInfo pubParasInfo;
        private static readonly ISysSettingDA sysSettingDA = DataAccess.CreateSysSettingDA();

        public CycleCountReleaseBLL()
        {
            this.pubParasInfo = null;
        }

        public CycleCountReleaseBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public CycleCountReleaseBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string DeleteCycleCountRelease(List<string> IDS)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string temp = string.Empty;
            string whLoginID = this.dataBaseID[0];
            try
            {
                List<CycleCountReleaseInfo> CycleCountReleaseInfos = new List<CycleCountReleaseInfo>();
                CycleCountReleaseInfo physicalRequestInfo = null;
                foreach (string id in IDS)
                {
                    physicalRequestInfo = CycleCountReleaseDA.GetCycleCountReleaseByID(dataBase, tran, id, whLoginID, this.pubParasInfo);
                    CycleCountReleaseInfos.Add(physicalRequestInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                CycleCountTagDA.DeleteCycleCountTagByReleaseID(dataBase, tran, IDS, whLoginID);
                if (CycleCountReleaseDA.DeleteCycleCountRelease(dataBase, tran, IDS, whLoginID) > 0)
                {
                    for (int i = 0; i < IDS.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<CycleCountReleaseInfo>(dataBase, tran, CycleCountReleaseInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "CYCLE_COUNT_RELEASE", whLoginID);
                        temp = temp + IDS[i] + ",";
                    }
                    if (string.IsNullOrEmpty(temp))
                    {
                        temp = temp.Substring(0, temp.Length - 1);
                    }
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return temp;
        }

        public DataSet GetAllCycleCountRelease()
        {
            return CycleCountReleaseDA.GetAllCycleCountRelease(this.dataBaseID[0], this.pubParasInfo);
        }

        public DataSet GetCycleCountReleaseByQueryEntity(CycleCountReleaseQueryEntity RequestEntity)
        {
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(RequestEntity.ReleaseID))
            {
                sqlWhere.Add(" CCR.RELEASE_ID " + RequestEntity.ReleaseID);
            }
            if (!string.IsNullOrEmpty(RequestEntity.OwnerID))
            {
                sqlWhere.Add(" CCT.OWNER_ID " + RequestEntity.OwnerID);
            }
            if (!string.IsNullOrEmpty(RequestEntity.SkuID))
            {
                sqlWhere.Add(" CCT.SKU_ID" + RequestEntity.SkuID);
            }
            if (!string.IsNullOrEmpty(RequestEntity.Loc))
            {
                sqlWhere.Add(" CCT.Loc" + RequestEntity.Loc);
            }
            if (!string.IsNullOrEmpty(RequestEntity.CreatedBy))
            {
                sqlWhere.Add(" CCR.CREATED_BY " + RequestEntity.CreatedBy);
            }
            if (!string.IsNullOrEmpty(RequestEntity.ReleaseDate))
            {
                sqlWhere.Add(RequestEntity.ReleaseDate.Replace("@ReleaseDate@", " CCR.RELEASE_DATE "));
            }
            return CycleCountReleaseDA.GetCycleCountReleaseByQueryList(sqlWhere, RequestEntity, this.dataBaseID[0], this.pubParasInfo);
        }

        public CycleCountReleaseInfo GetCycleCountReleaseInfoByID(string id)
        {
            string whLoginID = this.dataBaseID[0];
            CycleCountReleaseInfo cycleCountReleaseInfo = CycleCountReleaseDA.GetCycleCountReleaseByID(id, whLoginID, this.pubParasInfo);
            if (cycleCountReleaseInfo != null)
            {
                cycleCountReleaseInfo.CycleCountTagInfos = CycleCountTagDA.GetCycleCountTagByReleaseID(id, whLoginID, this.pubParasInfo);
                if (cycleCountReleaseInfo.CycleCountTagInfos == null)
                {
                    return cycleCountReleaseInfo;
                }
                if (!(sysSettingDA.GetSysSettingByID("CCSetCountQtySameAsSysQty", whLoginID, this.pubParasInfo).Value == "Y"))
                {
                    return cycleCountReleaseInfo;
                }
                foreach (CycleCountTagInfo info in cycleCountReleaseInfo.CycleCountTagInfos)
                {
                    if (info.Status == CycleCountTagStatus.New)
                    {
                        info.CountQty = info.SysQty;
                    }
                }
            }
            return cycleCountReleaseInfo;
        }

        public string InsertCycleCountRelease(CycleCountReleaseInfo obj)
        {
            string whLoginID = this.dataBaseID[0];
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string ReleaseID = string.Empty;
            try
            {
                if (string.IsNullOrEmpty(obj.ReleaseID))
                {
                    obj.ReleaseID = commonMethod.GenNextNumStr(dataBase, tran, "CYCLE_COUNT_RELEASE", whLoginID);
                }
                obj.UpdatedBy = obj.CreatedBy;
                obj.CreatedDate = DateTime.Now;
                obj.UpdatedDate = DateTime.Now;
                if (CycleCountReleaseDA.InsertCycleCountRelease(dataBase, tran, obj, whLoginID) > 0)
                {
                    ReleaseID = obj.ReleaseID;
                    new AuditDataBLL().InsertAuditData<CycleCountReleaseInfo>(dataBase, tran, null, obj, AuditAction.Insert, this.pubParasInfo.UserID, "CYCLE_COUNT_RELEASE", this.dataBaseID[0]);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return ReleaseID;
        }

        public string UpdateCycleCountRelease(CycleCountReleaseInfo obj)
        {
            string whLoginID = this.dataBaseID[0];
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string ReleaseID = string.Empty;
            try
            {
                CycleCountReleaseInfo oldPhysicalRequestInfo = CycleCountReleaseDA.GetCycleCountReleaseByID(dataBase, tran, obj.ReleaseID, this.dataBaseID[0], this.pubParasInfo);
                if (CycleCountReleaseDA.UpdateCycleCountRelease(dataBase, tran, obj, whLoginID) > 0)
                {
                    ReleaseID = obj.ReleaseID;
                    new AuditDataBLL().InsertAuditData<CycleCountReleaseInfo>(oldPhysicalRequestInfo, obj, AuditAction.Update, this.pubParasInfo.UserID, "CYCLE_COUNT_RELEASE", this.dataBaseID[0]);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return ReleaseID;
        }
    }
}

